package com.wxzl.community.common.store

import androidx.annotation.Keep
import com.tencent.mmkv.MMKV
import com.wxzl.community.common.data.UserInfo

@Keep
object UserInfoStore {

    private const val KEY_USER_INFO = "userInfo"

    fun isLogin(): Boolean {
        return MMKV.defaultMMKV()!!.containsKey(KEY_USER_INFO)
    }

    fun saveUserInfo(user: UserInfo) {
        user.accessToken.let {
            setToken(it)
        }

        MMKV.defaultMMKV()!!.encode(KEY_USER_INFO, user)

    }

    fun getUserInfo(): UserInfo? {
        return MMKV.defaultMMKV()!!.decodeParcelable(KEY_USER_INFO, UserInfo::class.java)

    }

    /**
     * 是否是业主
     */
    fun isOwner(): Boolean{
        return getUserInfo() != null && getUserInfo()!!.isOwner()
    }

    /**
     * 是否添加房产
     */
   fun hasHouse(): Boolean{
       return getUserInfo() != null && getUserInfo()!!.hasHouse()
   }

    /**
     * 是否选择小区
     */
    fun isSelectCommunity(): Boolean{
        return getUserInfo() != null && getUserInfo()!!.isSelectCommunity()
    }

    /**
     * 是否已认证
     */
    fun isCertified(): Boolean{
        return getUserInfo() != null && getUserInfo()!!.isCertified()
    }


    fun clearUserInfo() {
        MMKV.defaultMMKV()!!.removeValueForKey("TOKEN")
        MMKV.defaultMMKV()!!.removeValueForKey(KEY_USER_INFO)
        MMKV.defaultMMKV()!!.removeValueForKey("password")

    }

    fun setToken(token: String) {
        MMKV.defaultMMKV()!!.encode("TOKEN", token)

    }

    fun getToken(): String? {
        return MMKV.defaultMMKV()!!.decodeString("TOKEN")

    }


    fun setUsername(s: String) {
        MMKV.defaultMMKV()!!.encode("username", s)
    }

    fun setPassword(s: String) {
        MMKV.defaultMMKV()!!.encode("password", s)
    }

    fun getUsername(): String? {
        return MMKV.defaultMMKV()!!.decodeString("username", "")
    }

    fun getPassword(): String? {
        return MMKV.defaultMMKV()!!.decodeString("password")
    }





    fun saveUserStatus(userStatus: Int) {
        MMKV.defaultMMKV()!!.encode("userStatus", userStatus)

    }


    fun setAgree(b: Boolean) {
        MMKV.defaultMMKV()!!.encode("agree", b)
    }

    fun getAgree(): Boolean {
        return MMKV.defaultMMKV()!!.decodeBool("agree", true)
    }






    fun setSelectedBuildingId(id: Long) {
        MMKV.defaultMMKV()!!.encode("buildingId", id)
    }


    fun setCityName(cityname: String?) {
        MMKV.defaultMMKV()!!.encode("cityName", cityname)
    }

    fun getCityName(): String? {
        return MMKV.defaultMMKV()!!.decodeString("cityName")
    }

    fun setSelectedHomeId(id: Long) {
        MMKV.defaultMMKV()!!.encode("houseId", id)
    }

    fun getSelectedHomeId(): Long {
        return MMKV.defaultMMKV()!!.decodeLong("houseId")
    }

    fun setUserType(i: Int) {
        MMKV.defaultMMKV()!!.encode("type", i)
    }

    fun getUserType(): Int {
        return MMKV.defaultMMKV()!!.decodeInt("type")
    }


    fun setcompletionInfo(s: String) {
        MMKV.defaultMMKV()!!.encode("completion", s)
    }

    fun getcompletionInfo(): String? {
        return MMKV.defaultMMKV()!!.decodeString("completion")
    }

    fun readVisitorDescription(): Boolean {
        return MMKV.defaultMMKV()!!.decodeBool("readVisitorDescription", false)
    }

    fun setReadVisitorDescription() {
        MMKV.defaultMMKV()!!.encode("readVisitorDescription", true)
    }
}